//头文件
#include "sentry_interfaces/msg/robot_hp.hpp"

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "behaviortree_cpp/behavior_tree.h"
#include "behaviortree_cpp/bt_factory.h"
using std::placeholders::_1;
using namespace std::chrono_literals;
#ifndef RM_BT_H
#define RM_BT_H

//值的定义
std::string GetHP_value="";
sentry_interfaces::msg::RobotHP other_HP_value;
std::string my_HP_pub_value="";
std::string my_HP_value="";
std::string HP_max_value="";
std::string HP_min_value="";

//创建ros对行为树的修饰器
class ROS2DecoratorNode  : public BT::SyncActionNode
{
public:
  ROS2DecoratorNode(const std::string& name, const BT::NodeConfig& config) :
    BT::SyncActionNode(name, config)
  {}

  BT::NodeStatus tick() override
  {
    //端口映射
    setOutput("other_HP", other_HP_value);
    getInput("my_HP_pub", my_HP_pub_value);

    return BT::NodeStatus::SUCCESS;
  }


  static BT::PortsList providedPorts()
  {
    
    return {
      //端口注册
    BT::OutputPort<sentry_interfaces::msg::RobotHP>("other_HP"),
    BT::InputPort<std::string>("my_HP_pub"),

    };
  }
};


class RM_bt : public rclcpp::Node 
{
  public:
    RM_bt():Node("RM_bt") 
    {
    RCLCPP_INFO(this->get_logger(), "RM_bt,go");
    //创建订阅方

    subscription_other_HP =this->create_subscription<sentry_interfaces::msg::RobotHP>("other_HP", 10,std::bind(&RM_bt::topic_callback_other_HP, this, std::placeholders::_1));


    //创建发布方

    publisher_my_HP_pub = this->create_publisher<std_msgs::msg::String>("my_HP_pub", 10);
    timer_my_HP_pub = this->create_wall_timer(500ms, std::bind(&RM_bt::timer_callback_my_HP_pub, this));


    }
    //回调函数

    void topic_callback_other_HP(const sentry_interfaces::msg::RobotHP & msg)
    {
      other_HP_value=msg;
    }


    //定时器

    void timer_callback_my_HP_pub()
    {
      auto message = std_msgs::msg::String();
      message.data = my_HP_pub_value;
      publisher_my_HP_pub->publish(message);
    }


  private:
    //初始化订阅方

    rclcpp::Subscription<sentry_interfaces::msg::RobotHP>::SharedPtr subscription_other_HP;

   
    //初始化发布方

    rclcpp::TimerBase::SharedPtr timer_my_HP_pub;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_my_HP_pub;

};

#endif